from uuid import uuid4
from extensions import db


class Hotspot(db.Model):
    __tablename__ = "hotspots"
    id = db.Column(
        db.String(36), primary_key=True, default=lambda: str(uuid4()), nullable=False
    )
    # 用于排序的字段，值越小越靠前
    order = db.Column(db.Integer, nullable=False)
    # 热点标题
    title = db.Column(db.String(255), nullable=False)
    url = db.Column(db.String(512), nullable=False)
    # 平台标识 例如 sspai(少数派)、weibo(微博)、zhihu(知乎)等
    platform = db.Column(db.String(50), nullable=False, index=True)
    # 平台内信息来源板块标识 例如 少数派的「推荐」板块 
    section = db.Column(db.String(50), nullable=True, index=True)
    # 抓取时间戳
    crawl_at = db.Column(db.Integer, nullable=False, index=True)
    # 额外信息内容，如少数派的intro(文章前言)，photo_url(图片)
    extra_content = db.Column(db.JSON, nullable=True)

    def __repr__(self):
        return f"<Hotspot {self.title} {self.platform}>"
    
    def to_dict(self):
        """将模型转换为字典"""
        return {
            "id": self.id,
            "order": self.order,
            "title": self.title,
            "url": self.url,
            "platform": self.platform,
            "section": self.section,
            # 时间戳转换为字符串格式
            "crawl_at": str(self.crawl_at),
            "extra_content": self.extra_content
        }
